package com.cloudcreativity.blyg.ui;

import android.app.Activity;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.cloudcreativity.blyg.base.BaseDialogImpl;
import com.cloudcreativity.blyg.base.BaseModel;
import com.cloudcreativity.blyg.databinding.ActivityTestBinding;
import com.cloudcreativity.blyg.utils.speech.Auth;
import com.cloudcreativity.blyg.utils.LogUtils;
import com.cloudcreativity.blyg.utils.speech.OfflineResource;
import com.cloudcreativity.blyg.utils.ToastUtils;

import java.io.IOException;

public class TestModel extends BaseModel<Activity, ActivityTestBinding> {
    public ObservableField<String> text  =new ObservableField<>();


    /**
     * 发布时请替换成自己申请的appId appKey 和 secretKey。注意如果需要离线合成功能,请在您申请的应用中填写包名。
     * 本demo的包名是com.baidu.tts.sample，定义在build.gradle中。
     */
    protected String appId;

    protected String appKey;

    protected String secretKey;

    protected String sn; // 纯离线合成SDK授权码；离在线合成SDK免费，没有此参数

    // TtsMode.MIX; 离在线融合，在线优先； TtsMode.ONLINE 纯在线； TtsMode.OFFLINE 纯离线合成，需要纯离线SDK
    protected TtsMode ttsMode = TtsMode.OFFLINE;

    // 离线发音选择，VOICE_FEMALE即为离线女声发音。
    // assets目录下bd_etts_common_speech_m15_mand_eng_high_am-mix_vXXXXXXX.dat为离线男声模型文件；
    // assets目录下bd_etts_common_speech_f7_mand_eng_high_am-mix_vXXXXX.dat为离线女声模型文件;
    // assets目录下bd_etts_common_speech_yyjw_mand_eng_high_am-mix_vXXXXX.dat 为度逍遥模型文件;
    // assets目录下bd_etts_common_speech_as_mand_eng_high_am_vXXXX.dat 为度丫丫模型文件;

    // ===============初始化参数设置完毕，更多合成参数请至getParams()方法中设置 =================

    // 主控制类，所有合成控制方法从这个类开始
    private SpeechSynthesizer synthesizer;

    TestModel(final Activity context, ActivityTestBinding binding, BaseDialogImpl baseDialog) {
        super(context, binding, baseDialog);
        try {
            Auth.getInstance(context);
        } catch (Auth.AuthCheckException e) {
            ToastUtils.showShortToast(context,e.getLocalizedMessage());
            return;
        }

        appId = Auth.getInstance(context).getAppId();
        appKey = Auth.getInstance(context).getAppKey();
        secretKey = Auth.getInstance(context).getSecretKey();
        sn = Auth.getInstance(context).getSn(); // 纯离线合成必须有此参数；离在线合成SDK免费，没有此参数

        synthesizer = SpeechSynthesizer.getInstance();
        synthesizer.setParam(SpeechSynthesizer.PARAM_AUTH_SN,sn);

        synthesizer.setContext(context);
        synthesizer.setApiKey(appKey,secretKey);
        synthesizer.setAppId(appId);

        synthesizer.auth(ttsMode);

        //授权为离线模式
        synthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME,"15");
        synthesizer.setParam(SpeechSynthesizer.PARAM_SPEED,"4");

        // 离线资源文件， 从assets目录中复制到临时目录，需要在initTTs方法前完成
        OfflineResource offlineResource = createOfflineResource(OfflineResource.VOICE_FEMALE);

        // 声学模型文件路径 (离线引擎使用), 请确认下面两个文件存在
        synthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE,offlineResource.getTextFilename());
        synthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE,offlineResource.getModelFilename());

        synthesizer.setSpeechSynthesizerListener(new SpeechSynthesizerListener() {
            @Override
            public void onSynthesizeStart(String s) {
                LogUtils.e("xuxiwu","开始异步"+s);
            }

            @Override
            public void onSynthesizeDataArrived(String s, byte[] bytes, int i, int i1) {
                LogUtils.e("xuxiwu","异步接受数据"+s);
            }

            @Override
            public void onSynthesizeFinish(String s) {
                LogUtils.e("xuxiwu","异步结束"+s);
            }

            @Override
            public void onSpeechStart(String s) {
                LogUtils.e("xuxiwu","开始讲话"+s);
            }

            @Override
            public void onSpeechProgressChanged(String s, int i) {

            }

            @Override
            public void onSpeechFinish(String s) {
                LogUtils.e("xuxiwu","讲话结束"+s);
            }

            @Override
            public void onError(String s, SpeechError speechError) {
                LogUtils.e("xuxiwu","系统异常"+s);
                LogUtils.e("xuxiwu",speechError.description);
            }
        });
        int i = synthesizer.initTts(ttsMode);
        if(i!=0){
            ToastUtils.showShortToast(context,"初始化失败");
        }

    }

    @Override
    protected void initView() {

    }

    private OfflineResource createOfflineResource(String voiceType) {
        OfflineResource offlineResource = null;
        try {
            offlineResource = new OfflineResource(context, voiceType);
        } catch (IOException e) {
            // IO 错误自行处理
            e.printStackTrace();
        }
        return offlineResource;
    }

    @Override
    protected void initData() {

    }

    public void onTTSClick() {
        if (TextUtils.isEmpty(text.get())) return;
        if(synthesizer==null){
            return;
        }
        synthesizer.speak(text.get());
    }
}
